هر چهار متد، اکنون فقط بخشهای متفاوت (فراخوانی مدل و ذخیره پاسخ) را پیادهسازی میکنند. توجه به Role = XAiChatRole.Assistant در AskAsEnumerable که N-07 را برطرف میکند.
AskAsync
public virtual async Task<XAiMessageDto> AskAsync(
string prompt,
string type = null,
int? topK = null,
float? minScore = null,
Guid? projectId = null,
Guid? conversationId = null,
string connectionId = null,
IFormFileCollection files = null,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default)
{
validationProvider.GroupValidationBuilder()
.AddNotEmpty(prompt)
.AddNotNull(userInfo)
.AddNotEmpty(userInfo.UserId)
.ValidateGroup();
var prep = await PrepareChatContextAsync(
prompt, type, topK, minScore,
projectId, conversationId, connectionId,
files, userInfo,
resolveConversation: true,
persistPrompt: true,
cancellationToken: cancellationToken);
using var client = Descriptor.GetClient();
var response = await client.GetResponseAsync(Options, prep.History, cancellationToken);
if (!response.IsValid())
{
client.Dispose();
XException.ActionFailed.Throw();
}
return await dataProvider.AddMessage(
item: new XAiMessageDto
{
Content = response.Text,
OwnerId = userInfo.UserId,
CreatedOn = DateTime.UtcNow,
Role = XAiChatRole.Assistant,
ConversationId = prep.Conversation.Id,
ConversationTitle = prep.Conversation.Title,
},
connectionId: connectionId,
conversationId: prep.Conversation.Id,
cancellationToken: cancellationToken);
}
AskAsEnumerable — با اصلاح N-07
public virtual async IAsyncEnumerable<XAiResonseMessage> AskAsEnumerable(
string prompt,
string type = null,
int? topK = null,
float? minScore = null,
Guid? projectId = null,
Guid? conversationId = null,
string connectionId = null,
IFormFileCollection files = null,
XUserClaimsInfoDto userInfo = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
validationProvider.GroupValidationBuilder()
.AddNotEmpty(prompt)
.AddNotNull(userInfo)
.AddNotEmpty(userInfo.UserId)
.ValidateGroup();
var prep = await PrepareChatContextAsync(
prompt, type, topK, minScore,
projectId, conversationId, connectionId,
files, userInfo,
resolveConversation: true,
persistPrompt: true,
cancellationToken: cancellationToken);
using var client = Descriptor.GetClient();
var stream = client.GetStreamingResponseAsync(Options, prep.History);
var answer = new System.Text.StringBuilder();
long sequence = 1;
await foreach (var res in stream)
{
if (cancellationToken.IsCancellationRequested)
yield break;
answer.Append(res.Text);
yield return new XAiResonseMessage
{
Content = res.Text,
Sequence = sequence++,
OwnerId = userInfo.UserId,
Role = XAiChatRole.Assistant, // ← N-07 اصلاح شد
RespondTo = prep.PersistedPromptMessage.Id,
RespondedOn = DateTime.UtcNow,
};
}
// ذخیره پاسخ نهایی با نقش Assistant (N-07)
await dataProvider.AddMessage(
item: new XAiMessageDto
{
Content = answer.ToString(),
OwnerId = userInfo.UserId,
Role = XAiChatRole.Assistant,
CreatedOn = DateTime.UtcNow,
ConversationId = prep.Conversation.Id,
},
connectionId: connectionId,
conversationId: prep.Conversation.Id,
cancellationToken: cancellationToken);
}
AskTextFromKnowledgeAsync
public virtual async Task<string> AskTextFromKnowledgeAsync(
string prompt,
string type = null,
int? topK = null,
float? minScore = null,
XUserClaimsInfoDto userInfo = null,
CancellationToken cancellationToken = default)
{
validationProvider.GroupValidationBuilder()
.AddNotEmpty(prompt)
.AddNotNull(userInfo)
.AddNotEmpty(userInfo.UserId)
.ValidateGroup();
var prep = await PrepareChatContextAsync(
prompt, type, topK, minScore,
projectId: null, conversationId: null, connectionId: null,
files: null, userInfo,
resolveConversation: false,
persistPrompt: false,
cancellationToken: cancellationToken);
using var client = Descriptor.GetClient();
var response = await client.GetResponseAsync(Options, prep.History, cancellationToken);
if (!response.IsValid())
{
client.Dispose();
XException.ActionFailed.Throw();
}
return response.Text;
}
AskTextFromKnowledgeAsEnumerable
public virtual async IAsyncEnumerable<string> AskTextFromKnowledgeAsEnumerable(
string prompt,
string type = null,
int? topK = null,
float? minScore = null,
XUserClaimsInfoDto userInfo = null,
[EnumeratorCancellation] CancellationToken cancellationToken = default)
{
validationProvider.GroupValidationBuilder()
.AddNotEmpty(prompt)
.AddNotNull(userInfo)
.AddNotEmpty(userInfo.UserId)
.ValidateGroup();
var prep = await PrepareChatContextAsync(
prompt, type, topK, minScore,
projectId: null, conversationId: null, connectionId: null,
files: null, userInfo,
resolveConversation: false,
persistPrompt: false,
cancellationToken: cancellationToken);
using var client = Descriptor.GetClient();
var stream = client.GetStreamingResponseAsync(Options, prep.History);
await foreach (var res in stream)
{
if (cancellationToken.IsCancellationRequested)
yield break;
yield return res.Text;
}
}